<?php

use Common\AccessToken4Api;
use models\DAO\MemberModel;
use models\Exception\Business\MemberException;
use Yaf\Registry;
use models\Business\SmsCode;
use models\Business\Member;


class MemberController extends ApiBaseController
{
    /**
     * @SWG\Post(
     *     path="/v3/member/register",
     *     tags={"会员接口"},
     *     summary="会员注册接口",
     *     description="
     * mobile 必须参数 手机号码
     * code 必须参数 短信验证
     * password 必须参数 密码
     * registration_id 非必须参数 激光推送的设备号
     * device_type 必须参数 设备类型(1为IOS,2为Android,默认为1)
     * device_no 非必须参数 手机唯一标识符
     * invite_code 非必须参数 邀请者的uuid",
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberSingleData")
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="签名不存在或无效签名"
     *     )
     * )
     */
    public function registerAction() {
        $parameters = Registry::get('parameters');

        // 验证短信验证码
        SmsCode::verifySmsCode($parameters['mobile'], $parameters['code']);

        // 注册
        $member = Member::register($parameters);

        return $this->_responseJson(['data' => $member]);
    }

    /**
     * @SWG\Post(
     *     path="/v3/member/login",
     *     tags={"会员接口"},
     *     summary="会员登陆接口",
     *     description="
     * mobile 必须参数 手机号码
     * password 必须参数 密码
     * registration_id 非必须参数 激光推送的设备号
     * device_type 非必须参数 设备类型(1为IOS,2为Android,默认为1)
     * code 非必须参数 短信验证
     * device_no 非必须参数 手机唯一标识符",
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberSingleData")
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="签名不存在或无效签名"
     *     )
     * )
     */
    public function loginAction() {
        $parameters = Registry::get('parameters');

        // 验证短信验证码
        if (isset($parameters['code']))
            SmsCode::verifySmsCode($parameters['mobile'], $parameters['code'], 'login');

        $member = Member::login($parameters);

        // 获取新增会员
        $member['friend_today_increment'] = Member::getFriendIncrementByDate($member['id']);

        return $this->_responseJson(['data' => $member]);
    }

    /**
     * @SWG\Put(
     *     path="/v3/member/{id}/edit",
     *     tags={"会员接口"},
     *     summary="会员编辑个人资料接口",
     *     description="
     * nickname 非必须参数 修改昵称时必须
     * avatar 非必须参数 修改头像时必须
     * password 非必须参数 修改密码时必须
     * paypassword 非必须参数 设置或修改支付密码时必须
     * code 非必须参数 修改密码、设置或修改支付密码时必须",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="登陆接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction() {
        $parameters = Registry::get('parameters');
        $parameters['http_origin'] = $this->getRequest()->getServer('HTTP_ORIGIN');

        if(isset($parameters['password'])||isset($parameters['paypassword'])){
            if(!isset($parameters['code']))
                throw new MemberException(MemberException::GENERATE_SMS_CODE_EMPTY, MemberException::GENERATE_SMS_CODE_EMPTY_NO);
        }
        // 验证短信验证码
        if (isset($parameters['code'])&&isset($parameters['password']))
            SmsCode::verifySmsCode(Registry::get('jwt_info')->jti, $parameters['code'], 'resetpassword');
        if (isset($parameters['code'])&&isset($parameters['paypassword']))
            SmsCode::verifySmsCode(Registry::get('jwt_info')->jti, $parameters['code'], 'paypassword');

        $member = Member::editMember($parameters);

        return $this->_responseJson(['data' => $member]);

    }

    /**
     * @SWG\Get(
     *     path="/v3/member/direct",
     *     tags={"会员接口"},
     *     summary="获取当前登陆会员的直属好友列表",
     *     description="
     * page 必须参数 页码
     * page_size 必须参数 每页显示条数
     * sort 必须参数 排序方式 固定值:asc升序
     * order 必须参数 排序字段 固定值:id。",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function directAction() {
        $parameters = Registry::get('parameters');
        $parameters['parent'] = Registry::get('jwt_info')->id;

        $members = Member::getMemberList($parameters);

        return $this->_responseJson($members);
    }

    /**
     * @SWG\Get(
     *     path="/v3/member/indirect",
     *     tags={"会员接口"},
     *     summary="获取当前登陆会员的间属好友列表",
     *     description="
     * page 必须参数 页码
     * page_size 必须参数 每页显示条数
     * sort 必须参数 排序方式 固定值:asc升序
     * order 必须参数 排序字段 固定值:id。",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function indirectAction() {
        $parameters = Registry::get('parameters');
        $parameters['parent'] = Registry::get('jwt_info')->id;

        $members = Member::getAllIndirectMember($parameters);

        return $this->_responseJson($members);
    }

    public function statisticsAction() {

    }

    /**
     * @SWG\Post(
     *     path="/v3/member/forgetpwd",
     *     tags={"会员接口"},
     *     summary="忘记密码",
     *     description="
     * mobile 必须参数 忘记密码的手机号码
     * password 必须参数 修改密码时必须
     * code 必须参数 短信验证码",
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function forgetPwdAction() {
        $parameters = Registry::get('parameters');

        SmsCode::verifySmsCode($parameters['mobile'], $parameters['code'], 'forgetPwd');

        $member_id = Member::getMemberIdByMobile($parameters['mobile']);

        $member = Member::editMember(['password' => $parameters['password'], 'id' => $member_id]);

        return $this->_responseJson($member, '000', '密码重置成功');
    }

    /**
     * @SWG\Get(
     *     path="/v3/member/{id}/info",
     *     tags={"会员接口"},
     *     summary="获取指定会员的个人资料",
     *     description="
     * timestamp 必须参数 当前时间戳",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="登陆接口返回的id字段的值 当id=0时，表示获取当前登录会员的会员信息,返回结果中会有刷新过的jwt，反之则表示获取指定会员编号的会员信息，返回结果中jwt为空",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="所有参数使用&连接的rsa加密结果",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/memberSingleData")
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="签名不存在或无效签名"
     *     )
     * )
     */
    public function infoAction() {
        $parameters = Registry::get('parameters');
        $member = Member::getMemberInfoById($parameters['id'] != 0 ? $parameters['id'] : Registry::get('jwt_info')->id );
        if(!$member['id']){
            throw new MemberException(MemberException::LOGIN_FIRST, MemberException::LOGIN_FIRST_NO);
        }
        $member['jwt'] = $parameters['id'] !=0 && $parameters['id'] != Registry::get('jwt_info')->id ? '' :
            AccessToken4Api::generateJWT($member['mobile'], $member['id'], $member['parent'], $member['uuid']);

        // 获取当前用户是否绑定过淘宝第三方授权登录
        $member_oauth = MemberModel::getInstance()->query(
            "SELECT o.`id` FROM `oauth` AS o LEFT JOIN `memberoauth` AS mo ON o.`id` = mo.`oauth_id` WHERE o.`type` = 4 AND mo.`member_id` = " . $member['id']
        );
        $member['is_tb_oauth'] = (int)!empty($member_oauth);

        // 获取新增会员
        $member['friend_today_increment'] = Member::getFriendIncrementByDate($member['id']);

        return $this->_responseJson(['data' => $member]);
    }

}
